#!/usr/bin/env python3
import requests
import json
import base64
import os

def test_with_base64():
    # 选择一个测试图片
    image_path = "test/1.jpg"
    if not os.path.exists(image_path):
        print(f"图片不存在: {image_path}")
        return
        
    # 读取图片并编码为base64
    with open(image_path, 'rb') as f:
        image_data = f.read()
    
    b64_data = base64.b64encode(image_data).decode('utf-8')
    image_data_uri = f"data:image/jpeg;base64,{b64_data}"
    
    # 构造请求数据
    data = {
        "image_data": image_data_uri,
        "save": 1,
        "return_src": 1,
        "return_box": 1
    }
    
    print(f"测试图片: {image_path}")
    print("发送POST请求...")
    
    try:
        response = requests.post("http://localhost:8080", 
                               data=json.dumps(data),
                               headers={'Content-Type': 'application/json'},
                               timeout=30)
        
        print(f"状态码: {response.status_code}")
        print(f"响应头: {response.headers}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            try:
                result = response.json()
                if result.get('errcode') == 0:
                    print("✅ 车牌识别成功!")
                    data = result.get('data', [])
                    for i, plate in enumerate(data):
                        print(f"车牌 {i+1}: {plate}")
                else:
                    print(f"❌ 识别失败: {result}")
            except json.JSONDecodeError:
                print("❌ 响应不是有效的JSON格式")
        
    except Exception as e:
        print(f"请求失败: {e}")

if __name__ == "__main__":
    test_with_base64() 